import torch
from torch.autograd import Variable
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as func
from torchvision import datasets, transforms
import sys

LR = 1e-2
BATCH_SIZE = 64
EPOCHS = 20
MOMENTUM = 0.5

# W: input volume size, K: kernel size, P: padding, S: stride, D: Dilation
# convolutional layer output size = (W - K + 2P)/S + 1
# P = (K-1)/2 and S = 1 => input size == output size
# pooling layer output size: (W + 2P - K) / S + 1


class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=10, kernel_size=5)
        self.pool1 = nn.MaxPool2d(2, stride=2, dilation=1)
        self.conv2 = nn.Conv2d(10, 20, 5)
        self.pool2 = nn.MaxPool2d(2)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        # print('input: ', x.data.shape)
        x = self.conv1(x)
        # print('conv1: ', x.data.shape)
        x = func.relu(self.pool1(x))
        # print('pool1: ', x.data.shape)
        x = self.conv2_drop(self.conv2(x))
        # print('conv2: ', x.data.shape)
        x = func.relu(self.pool2(x))
        # print('pool2: ', x.data.shape)
        x = x.view(-1, 320)
        x = func.relu(self.fc1(x))
        x = func.dropout(x, training=self.training)
        x = func.relu(self.fc2(x))
        return func.log_softmax(x)


class SimpleCNN2(nn.Module):
    def __init__(self):
        super(SimpleCNN2, self).__init__()
        self.conv1 = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=10, kernel_size=5),
                                   nn.MaxPool2d(2),
                                   nn.ReLU())

        self.conv2 = nn.Sequential(nn.Conv2d(10, 20, 5),
                                   nn.Dropout2d(),
                                   nn.MaxPool2d(2),
                                   nn.ReLU())

        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        # print(x)
        x = self.conv1(x)
        x = self.conv2(x)
        print(x.data.shape)
        x = x.view(-1, 320)
        x = func.relu(self.fc1(x))
        x = func.dropout(x, training=self.training)
        x = func.relu(self.fc2(x))
        return func.log_softmax(x)


def train(model, opt, data_loader, epoch):
    model.train()

    for batch_idx, (data, target) in enumerate(data_loader):
        data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        opt.zero_grad()
        output = model(data)
        loss = func.nll_loss(output, target)
        loss.backward()
        opt.step()
        if batch_idx % 100 == 0:
            print(batch_idx)
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(data_loader.dataset),
                100. * batch_idx / len(data_loader), loss.data[0]
            ))


def test(model, data_loader):
    model.eval()
    test_loss = 0
    correct = 0
    for data, target in data_loader:
        data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)

        output = model(data)

        test_loss += func.nll_loss(output, target, size_average=False).data[0]
        pred = output.data.max(1, keepdim=True)[1]
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()

    test_loss /= len(data_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuary:{}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(data_loader.dataset),
        100. * correct / len(data_loader.dataset)
    ))


if __name__ == '__main__':
    data_dir = sys.argv[1]
    m = SimpleCNN().cuda()
    print(m)

    optimizer = optim.SGD(m.parameters(), lr=LR, momentum=MOMENTUM)

    # kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
    kwargs = {}
    train_loader = torch.utils.data.DataLoader(
        datasets.MNIST(data_dir, train=True, download=False, transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081, ))
        ])),
        batch_size=BATCH_SIZE, shuffle=True, **kwargs
    )

    test_loader = torch.utils.data.DataLoader(
        datasets.MNIST(data_dir, train=False, download=False, transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081, ))
        ])),
        batch_size=BATCH_SIZE, shuffle=True, **kwargs
    )

    for e in range(1, 10):
        train(m, optimizer, train_loader, e)
        test(m, test_loader)
